<template>
    <div>
        <h1>会员修改</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">会员名称</td>
                    <td><input type="text" v-model="info.HyName"></td>
                </tr>
                <tr>
                    <td align="right">图片</td>
                    <td>
                        <input type="file" @change="imgs">
                        <img :src="info.Img" width="60" height="60" >
                    </td>
                </tr>
                <tr>
                    <td align="right">会员等级</td>
                    <td><input type="text" v-model="info.GjId"></td>
                </tr>
                <tr>
                    <td align="right">年龄</td>
                    <td><input type="text" v-model="info.Age"></td>
                </tr>
                <tr>
                    <td align="right"></td>
                    <td><input type="button" value="修改" @click="ok"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref, reactive,onMounted} from 'vue'
import axios from 'axios';
import { useRouter,useRoute } from 'vue-router';
const router=useRouter();
const route=useRoute();
let info:any=reactive({
    Id:route.query.id,
  HyName:'',
  Img: '',
  GjId: '',
  Age: '',
})
onMounted(()=>{
    getFind();
})
//=======反填=======

const getFind=()=>{
    axios({
        url:'/api/HY/getfind',
        method:'get',
        params:{
            id:route.query.id
        }
    })
    .then((res)=>{
        console.log(res);
        Object.assign(info,res.data)
    })
    .catch((err)=>{
        console.log(err);
        
    })
}
const imgs=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj)
    axios({
        url:'/api/HY/Img',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="不是图片")
    {
            alert("你上传的不是图片");
            return;
    }
    if(res.data=="大于2M")
    {
            alert("你上传的大于2M");
            return;
    }
    info.Img=res.data;
    })
    .catch((err)=>{
        console.log(err);
        
    })
}
const ok=()=>{
    if(info.Name=="")
{
        alert("会员名称不能为空");
        return;
};
    axios({
        url:'/api/HY/Update',
        method:'put',
        data:info
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1)
    {
        alert("会员名称重复");
        return;
    }
    if(res.data>0)
    {
        alert("修改成功");
        router.push('/hylist')
        return;
    }
    else
    {
        alert("修改失败");
        return;
    }
        
    })
}
</script>

<style scoped>

</style>